//
//  ViewController.m
//  24加密解密作业
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+SYMD5.h"
#import "NSString+SYDES.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *jiemiTextField;
@property (weak, nonatomic) IBOutlet UITextField *jiamiTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    MD5 API使用
//    通过POST请求, 实现加密和解密过程
//    MD5加密
//    MD5解密
//    界面要求 加密按钮 和 解密按钮
    
//    MD5解密 POST请求接口发送 参数格式
//    secret=d24e17eda8790518f06253e301717186 =号后面为MD5字符串
//    MD5加密 POST请求接口发送 参数格式
//    text=Hello =号后为要加密的字符串
//    
//    需求1: 发送加密时, 会返回对应text内容的32和16的MD5串, 将字符串显示在解密textField中
//    需求2: 发送解密时, 会返回对应secret MD5串的文本, 将文本内容显示在加密的textField中
//    需求3: 发送解密时, 会有找不到字符串的情况。 这时, 发送一次加密, 将需要解密内容的源字符串, 发送一次, 使服务器保留有该字符串后, 再次进行解密。
    
    
    
//    http://tool.zzblo.com/api/md5/encrypt
//    http://tool.zzblo.com/api/md5/decrypt
    
    NSString *str = @"123456hjkhkjhkhkj";
//    NSString *finalStr = [str stringWith32BitMD5];
//    NSLog(@"%@",finalStr);
    
    NSString *sourceString = [str stringWithDES];
//        NSLog(@"%@",sourceString);
    NSString *jiemiString = [sourceString desWithString];
//    NSLog(@"%@",jiemiString);
    
    
    [self networkPost];
    [self jieminetworkPost];
}

- (void)networkPost
{
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    NSString *postString = @"http://tool.zzblo.com/api/md5/encrypt";
    
    NSString *parmeters = [NSString stringWithFormat:@"text=%@",_jiamiTextField.text];
    
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    [manager POST:postString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解密成功:%@",responseObject);
        _jiamiTextField.text = responseObject[@"text"];
        _jiemiTextField = nil;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",task.currentRequest.URL);
        NSLog(@"失败:%@",error);
    }];
    
    
}

- (void)jieminetworkPost
{
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    NSString *postString = @"http://tool.zzblo.com/api/md5/decrypt";
    
    NSString *parmeters = [NSString stringWithFormat:@"secret=%@",_jiemiTextField.text];
    
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    [manager POST:postString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解密成功:%@",responseObject);
        _jiemiTextField.text = responseObject[@"text"];
        _jiamiTextField = nil;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",task.currentRequest.URL);
        NSLog(@"失败:%@",error);
    }];
   
}



- (IBAction)jiamiAction:(id)sender {
    NSString *str = _jiamiTextField.text;
    NSString *jiamiStr = [str stringWithDES];
    NSLog(@"%@",jiamiStr);
//    NSString *jiemiStr = [jiamiStr desWithString];
    _jiemiTextField.text = jiamiStr;
//    NSLog(@"%@",jiamiStr);
    
}

- (IBAction)jiemiAction:(id)sender {
    NSString *str = _jiemiTextField.text;
    NSString *jiemiStr = [str stringWithDES];
    NSLog(@"%@",jiemiStr);
    NSString *jiamiStr = [jiemiStr desWithString];
    _jiamiTextField.text = jiamiStr;
    NSLog(@"%@",jiamiStr);
   
}

- (IBAction)zhuanghuanMD5Action:(id)sender {
    
    NSString *str = _jiamiTextField.text;
    NSString *jiamiStr = [str stringWith32BitMD5];
    _jiamiTextField.text = jiamiStr;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
